import Vue from 'vue'
import VueRouter from 'vue-router'
import Layout from '@/layout'

Vue.use(VueRouter)

/* Layout */
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/dashboard/index'),
        meta: { title: '首页', icon: 'dashboard' }
      }
    ]
  },
  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  }
]

export const asyncRoutes = [
  {
    path: '/inventory',
    component: Layout,
    redirect: '/inventory/list',
    name: 'Inventory',
    meta: { title: '库存管理', icon: 'el-icon-goods' },
    children: [
      {
        path: 'list',
        name: 'InventoryList',
        component: () => import('@/views/inventory/list'),
        meta: { title: '库存列表' }
      },
      {
        path: 'in',
        name: 'InventoryIn',
        component: () => import('@/views/inventory/in'),
        meta: { title: '入库管理' }
      },
      {
        path: 'out',
        name: 'InventoryOut',
        component: () => import('@/views/inventory/out'),
        meta: { title: '出库管理' }
      }
    ]
  },
  {
    path: '/products',
    component: Layout,
    redirect: '/products/list',
    name: 'Products',
    meta: { title: '商品管理', icon: 'el-icon-shopping-bag-1' },
    children: [
      {
        path: 'list',
        name: 'ProductList',
        component: () => import('@/views/products/list'),
        meta: { title: '商品列表' }
      },
      {
        path: 'category',
        name: 'ProductCategory',
        component: () => import('@/views/products/category'),
        meta: { title: '商品分类' }
      }
    ]
  },
  {
    path: '/sales',
    component: Layout,
    redirect: '/sales/list',
    name: 'Sales',
    meta: { title: '销售管理', icon: 'el-icon-shopping-cart-full' },
    children: [
      {
        path: 'list',
        name: 'SalesList',
        component: () => import('@/views/sales/list'),
        meta: { title: '销售记录' }
      },
      {
        path: 'statistics',
        name: 'SalesStatistics',
        component: () => import('@/views/sales/statistics'),
        meta: { title: '销售统计' }
      }
    ]
  },
  {
    path: '/system',
    component: Layout,
    redirect: '/system/users',
    name: 'System',
    meta: { title: '系统管理', icon: 'el-icon-setting', roles: ['admin'] },
    children: [
      {
        path: 'users',
        name: 'Users',
        component: () => import('@/views/system/users'),
        meta: { title: '用户管理', roles: ['admin'] }
      },
      {
        path: 'roles',
        name: 'Roles',
        component: () => import('@/views/system/roles'),
        meta: { title: '角色管理', roles: ['admin'] }
      }
    ]
  },
  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new VueRouter({
  mode: 'history',
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher
}

export default router 